local dianmo = fk.CreateSkill {
  name = "rmt__dianmo",
}

Fk:loadTranslationTable{
  ["rmt__dianmo"] = "点墨",
  [":rmt__dianmo"] = "游戏开始时，准备阶段或当你每回合首次受到伤害后，你可以观看两个能转化/视为使用或打出牌的技能，选择其中一个获得（至多四个）或替换一个已有技能，"..
  "然后摸空置的技能数张牌。",

  ["#rmt__dianmo-get"] = "点墨：选择要获得的一个技能",
  ["#rmt__dianmo-replace"] = "点墨：是否替换现有的“点墨”技能？（上限4个，现有%arg个）",
  ["#rmt__dianmo-discard"] = "点墨：选择丢弃的“点墨”技能",
}

local spec = {
  on_use = function (self, event, target, player, data)
    local room = player.room
    local skills = {}
      -- 技能禁池
      local dianmoBanList = {
        --散设鬼谋序列（完全没有效果和描述）
        "sj3__guimou__1","sj3__guimou__2","sj3__guimou__3","sj3__guimou__4","sj3__guimou__5",
        "sj3__guimou__6","sj3__guimou__7","sj3__guimou__8","sj3__guimou__9"
      }
      for _, name in ipairs(room.general_pile) do
        local general = Fk.generals[name]
        for _, skill in ipairs(general:getSkillNameList()) do
          if Fk.skills[skill]:isInstanceOf(ViewAsSkill) and not table.contains(dianmoBanList, skill) then
            table.insertIfNeed(skills, skill)
          end
        end
      end
    local skl = table.filter(skills, function (s)
      return Fk.skills[s] and not player:hasSkill(s, true)
    end)
    local choice = room:askToChoices(player, {
      choices = table.random(skl, 2),
      min_num = 1,
      max_num = 1,
      skill_name = dianmo.name,
      prompt = "#rmt__dianmo-get",
      cancelable = false,
      detailed = true,
    })
    if choice == "" then
      choice = table.random(skl)
    else
      choice = choice[1]
    end
    local exist_skills = table.filter(player:getTableMark(dianmo.name), function (s)
      return player:hasSkill(s, true)
    end)
    if #exist_skills > 0 then
      if #exist_skills == 4 or
        room:askToSkillInvoke(player, {
          skill_name = dianmo.name,
          prompt = "#rmt__dianmo-replace:::"..#exist_skills,
        }) then
        local choice2 = room:askToChoices(player, {
          choices = exist_skills,
          min_num = 1,
          max_num = 1,
          skill_name = dianmo.name,
          prompt = "#rmt__dianmo-discard",
          cancelable = false,
          detailed = true,
          all_choices = exist_skills,
        })
        if choice2 == "" then
          choice2 = table.random(exist_skills)
        else
          choice2 = choice2[1]
        end
        table.removeOne(exist_skills, choice2)
        table.insert(exist_skills, choice)
        room:setPlayerMark(player, dianmo.name, exist_skills)
        room:handleAddLoseSkills(player, choice.."|-"..choice2)
        if #exist_skills < 4 and not player.dead then
          player:drawCards(4 - #exist_skills, dianmo.name)
        end
        return
      end
    end
    table.insertIfNeed(exist_skills, choice)
    room:setPlayerMark(player, dianmo.name, exist_skills)
    room:handleAddLoseSkills(player, choice)
    if #exist_skills < 4 and not player.dead then
      player:drawCards(4 - #exist_skills, dianmo.name)
    end
  end,
}

dianmo:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(dianmo.name) then
      local damage_events = player.room.logic:getActualDamageEvents(1, function(e)
        return e.data.to == player
      end, Player.HistoryTurn)
      return #damage_events == 1 and damage_events[1].data == data
    end
  end,
  on_use = spec.on_use,
})

dianmo:addEffect(fk.EventPhaseStart, {
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(dianmo.name) and player.phase == Player.Start
  end,
  on_use = spec.on_use,
})

dianmo:addEffect(fk.GameStart, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(dianmo.name)
  end,
  on_use = spec.on_use,
})

return dianmo
